package org.apache.ibatis.databases.blog;

import java.sql.*;

public class StoredProcedures {
    public static void selectTwoSetsOfTwoAuthors(int p1, int p2, ResultSet[] rs1, ResultSet[] rs2) throws SQLException {
        Connection conn = DriverManager.getConnection("jdbc:default:connection");
        PreparedStatement ps1 = conn.prepareStatement("SELECT * FROM author WHERE id IN (?,?)");
        ps1.setInt(1, p1);
        ps1.setInt(2, p2);
        rs1[0] = ps1.executeQuery();
        PreparedStatement ps2 = conn.prepareStatement("SELECT * FROM author WHERE id IN (?,?)");
        ps2.setInt(1, p2);
        ps2.setInt(2, p1);
        rs2[0] = ps2.executeQuery();
        conn.close();
    }

    public static void insertAuthor(int id, String username, String password, String email) throws SQLException {
        Connection conn = DriverManager.getConnection("jdbc:default:connection");
        try {
            PreparedStatement ps = conn.prepareStatement("INSERT INTO author (id, username, password, email) VALUES (?,?,?,?)");
            ps.setInt(1, id);
            ps.setString(2, username);
            ps.setString(3, password);
            ps.setString(4, email);
            ps.executeUpdate();
        } finally {
            conn.close();
        }
    }

    public static void selectAuthorViaOutParams(int id, String[] username, String[] password, String[] email, String[] bio) throws SQLException {
        Connection conn = DriverManager.getConnection("jdbc:default:connection");
        PreparedStatement ps = conn.prepareStatement("SELECT * FROM author WHERE id = ?");
        ps.setInt(1, id);
        ResultSet rs = ps.executeQuery();
        rs.next();
        username[0] = rs.getString("username");
        password[0] = rs.getString("password");
        email[0] = rs.getString("email");
        bio[0] = rs.getString("bio");
        conn.close();
    }
}
